<?php

namespace app\meizhuangapi\controller\meizhuang;

use app\meizhuangapi\controller\Controller;
use app\meizhuangapi\model\meizhuang\Ask as AskModel;
use think\Request;
use app\meizhuangapi\validate\Ask as AskValidate;
use app\storeapi\model\Goods as GoodsModel;
use app\meizhuangapi\model\meizhuang\GoodsSku as GoodsSkuModel;
use think\Db;

/**
 * 美妆-问大家控制器
 */
class Ask extends \app\meizhuangapi\controller\Controller{

    protected $askModel;
    protected $tag=[];

    public function _initialize(Request $request = null){
        $this->askModel=new AskModel();
        $this->tag=[
            '飞粉','正品','色号','质量','黑黄皮','适合淡妆','容易上妆','易过敏'
        ];
    }
    /*
     * 获取标签
     */
    public function getTag(){
        return $this->renderSuccess($this->tag,'获取成功');
    }
    /*
     * 提问
     */
    public function ask(){
        $user = $this->getUser(false);
        $data['user_id']=$user['user_id'];
        $data['wxapp_id']=input('post.wxapp_id');
        $data['goods_id']=input('post.goods_id');
        $data['question']=input('post.question');
        $data['status']=1;
        $data['update_time']=0;
        $data['type']=1;
        $data['upid']=0;
        $data['remark']=input('post.tag');
        $data['create_time']=time();
        $validate = new AskValidate;
        //表单验证
        if(!$validate->scene('add')->check($data)){
            return $this->renderError($validate->getError());
        }
        if($this->askModel->addQuestion($data)){
            return $this->renderSuccess([],'提问成功');
        }
    }

    /*
     * 历史提问list页面
     */
    public function getAskList(){
        $remark=input('post.tag');
        //商品详情，提问总数，提问标签，标签问题总数，问题列表
        $GoodsModel=new GoodsModel();
        $goodWhere['goods_id']=input('post.goods_id');
        $goodWhere['wxapp_id']=input('post.wxapp_id');
        $goodInfo=$GoodsModel->getGoodInfo($goodWhere);//商品详情
        //获取问题总数量
        $cou=$this->askModel->getCount(['goods_id'=>$goodWhere['goods_id'],'type'=>1]);
        $goodInfo['total']=$cou;
        $goodWhere['type']=1;
        $count=$this->askModel->getCount($goodWhere);//商品提问总数
        //获取提问标签并标记总数
        $tagCount=[];
        foreach ($this->tag as $k=>$v){
            $flagCount=$this->askModel->getCount(['goods_id'=>$goodWhere['goods_id'],'type'=>1,'remark'=>$v]);
            $tagCount[$k]['tag']=$v;
            $tagCount[$k]['count']=$flagCount;
        }
        //获取问题列表 和一条回答信息
        if(empty($remark)||$remark=='全部'){
            $where['goods_id']=$goodWhere['goods_id'];
        }else{
            $where['goods_id']=$goodWhere['goods_id'];
            $where['remark']=$remark;
        }
        $where['type']=1;
        $questionList=$this->askModel->getList($where);
        if($questionList){
            foreach ($questionList as $k=>$v){
                $arr=Db::name('meizhuang_ask')->where(['upid'=>$v['id'],'type'=>2])->find();
                $questionList[$k]['answer']=$arr;
            }
        }
        $return['goodsInfo']=$goodInfo;
        $return['count']=$count;
        $return['tagInfo']=$tagCount;
        $return['askList']=$questionList;
        return $this->renderSuccess($return,'获取成功');
    }
    /*
     * 获取提问详情
     */
    public function getInfo(){
        //获取某一个提问下的所有回复
        $question_id=input('post.question_id');//提问id
        $where['question_id']=$question_id;
        $list=$this->askModel->getList($where);
        $return=$this->getTree($list,0);

        $GoodsModel=new GoodsModel();
        $goodWhere['goods_id']=input('post.goods_id');
        $goodWhere['wxapp_id']=input('post.wxapp_id');
        $goodInfo=$GoodsModel->getGoodInfo($goodWhere);//商品详情
        //获取问题总数量
        $cou=$this->askModel->getCount(['goods_id'=>$goodWhere['goods_id'],'type'=>1]);
        $goodInfo['commitCount']=$cou;
        $returns['question']=$return;
        $returns['goodInfo']=$goodInfo;

        return $this->renderSuccess($returns,'获取成功');
    }
    /*
     * 问大家详情页树状结构
     */
    private function getTree($data,$upid){
        $tree=[];
        foreach($data as $k=>$v){
            //获取头像  昵称
            $user=Db::name('user')->where(['user_id'=>$v['user_id']])->find();
            if(!empty($user['username'])){
                $v['nickname']=$user['username'];
            }else{
                $v['nickname']=$user['nickName'];
            }
            $v['headImg']=$user['avatarUrl'];
            if($v['upid']==$upid){
                $v['next']=$this->getTree($data,$v['id']);
                $tree[]=$v;
            }
        }
        return $tree;
    }
    /*
     * 回答
     */
    public function answer(){
        $user = $this->getUser(false);
        $data['user_id']=$user['user_id'];
        $data['goods_id']=input('post.goods_id');
        $data['question']=input('post.question');
        $data['wxapp_id']=input('post.wxapp_id');
        $data['question_id']=input('post.question_id');
        $data['status']=1;
        $data['update_time']=0;
        $data['type']=2;
        $data['upid']=input('post.id');//上级id
        $re=Db::name('meizhuang_ask')->where(['id'=>$data['upid']])->find();
        $data['remark']=$re['remark'];//所属标签
        $data['create_time']=time();
        if($this->askModel->askQuestion($data)){
            return $this->renderSuccess([],'回复成功');
        }
        return $this->renderError([],'回复失败');
    }
    public function test(){
        $arr=getAgentInfo(7);
        var_dump($arr);
    }

    /*
    * 获取产品参数or产品规格
    */
    public function getGoodsSku(){
        $goods_id=input('post.goods_id');
        $type=input('post.type');//1:产品参数  2：产品规格
        if(empty($goods_id)){
            return $this->renderError([],'商品id不能为空');
        }
        $specrel=Db::name('goods_spec_rel')->where(['goods_id'=>$goods_id])->select();
        if(empty($specrel)){
            return $this->renderError([],'未获取到规格参数');
        }
        $newInfo=[];
        foreach ($specrel as $k=>$v){
            //查询规格名称
            $secInfo=Db::name('spec')->where(['spec_id'=>$v['spec_id']])->field('spec_id,spec_name')->find();
            //查询规格值
            $spec_value=Db::name('spec_value')->where(['spec_id'=>$v['spec_id']])->field('spec_id,spec_value,spec_value_id')->select();
            if($type==1){
                if(count($spec_value)==1){
                    $newInfo[]=[
                        'name'=>$secInfo['spec_name'],
                        'value'=>$spec_value[0]['spec_value']
                    ];
                }
            }else{
                if(count($spec_value)>1){
                    $a=[];
                    foreach ($spec_value as $k1=>$v1){
                        $a[]=$v1['spec_value'];
                    }
                    $newInfo[]=[
                        'name'=>$secInfo['spec_name'],
                        'value'=>$a
                    ];
                }
            }
        }
        return $this->renderSuccess($newInfo,'获取成功');
    }
    /*
     * 获取帮助列表
     */
    public function getHelpList(){
        //获取客服电话
        $wxapp_id=input('post.wxapp_id');
        if(empty($wxapp_id)){
            return $this->renderError([],'小程序id参数不能为空');
        }
        $phoneInfo=Db::name('store_shop')->where(['wxapp_id'=>$wxapp_id])->field('phone')->find();
        $info=Db::name('help')->where(['type'=>3])->order('sort','desc')->select();
        $newArray=[
            'phoneInfo'=>$phoneInfo['phone'],
            'listInfo'=>$info
        ];
        if($info){
            return $this->renderSuccess($newArray,'获取成功');
        }
        return $this->renderSuccess($newArray,'未获取到信息');
    }
    /*
     * 获取我的帮助详情
     */
    public function getHelpInfo(){
        $id=input('post.id');
        if(empty($id)){
            return $this->renderError([],'id参数不能为空');
        }
        $info=Db::name('help')->where(['id'=>$id])->find();
        if($info){
            $info['createtime']=date('Y-m-d H:i:s',$info['createtime']);
            $info['content']=htmlspecialchars_decode($info['content']);
            return $this->renderSuccess($info,'获取成功');
        }
        return $this->renderSuccess([],'未获取到信息');
    }
    /*
     * 美妆详情-热门推荐
     */
    public function getHot(){
        $wxapp_id=input('post.wxapp_id');
        //获取最新四条热门推荐数据
        $list=Db::name('goods')
            ->alias('g')
            ->join('yoshop_goods_sku s','g.goods_id = s.goods_id')
            ->where('g.is_hot','=',1)
            ->where('g.wxapp_id','=',$wxapp_id)
            ->field('g.goods_name,g.goods_id,s.goods_price')
            ->order('g.create_time desc')
            ->limit(4)
            ->select();
        $list=$list->toArray();
        if($list){
            foreach($list as $k=>$v){
                //获取图片地址,只获取一张图片的地址
                $imageId=Db::name('goods_image')->where(['goods_id'=>$v['goods_id']])->field('image_id')->find();
                $uploadInfo=Db::name('upload_file')->where(['file_id'=>$imageId['image_id']])->find();
                $list[$k]['imageUrl']=$uploadInfo['file_url'].'/'.$uploadInfo['file_name'];
            }
        }else{
            return $this->renderSuccess([],'未获取到信息');
        }
        return $this->renderSuccess($list,'获取成功');
    }
}
